package com.grape.domain;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.grape.constants.Constants;
import com.grape.utils.DateUtils;
import com.grape.utils.StringUtils;
import com.grape.utils.ThreadLocalUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.logging.log4j.util.Strings;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 *
 * 账单表 实体类
 *
 * @author      putao
 * @email       2628279194@qq.com
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("t_bill")
public class Bill extends BaseDomain implements Serializable {

    private static final long serialVersionUID = 1L;


    /**
     * 用户id
     */
    @ApiModelProperty("用户id")
    private String userId;
    /**
     * 金额
     */
    @NotNull(message = "金额不能为空")
    @ApiModelProperty("金额")
    private BigDecimal money;
    /**
     * 账单时间
     */
    @NotNull(message = "账单时间不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @ApiModelProperty("账单时间")
    private Date time;
    /**
     * 分类id
     */
    @NotBlank(message = "分类id不能为空")
    @ApiModelProperty("分类id")
    private String categoryId;
    /**
     * 分类名字
     */
    @NotBlank(message = "分类名字不能为空")
    @ApiModelProperty("分类名字")
    private String categoryName;
    /**
     * 分类图标
     */
    @NotBlank(message = "分类图标不能为空")
    @ApiModelProperty("分类图标")
    private String categoryIcon;
    /**
     * 分类类型（pay: 支出, income: 收入）
     */
    @NotBlank(message = "分类类型（pay: 支出, income: 收入）不能为空")
    @ApiModelProperty("分类类型（pay: 支出, income: 收入）")
    private String categoryType;
    /**
     * 分类app图标
     */
    @NotBlank(message = "分类app图标")
    @ApiModelProperty("分类app图标")
    private String categoryAppIcon;
    /**
     * 账本id
     */
    @NotBlank(message = "账本id不能为空")
    @ApiModelProperty("账本id")
    private String accountBookId;
    /**
     * 账本名字
     */
    @NotBlank(message = "账本名字不能为空")
    @ApiModelProperty("账本名字")
    private String accountBookName;
    /**
     * 账户id
     */
    @ApiModelProperty("账户id")
    private String accountId;
    /**
     * 账户名字
     */
    @ApiModelProperty("账户名字")
    private String accountName;
    /**
     * 账户图标
     */
    @ApiModelProperty("账户图标")
    private String accountIcon;
    /**
     * 账户图标颜色
     */
    @ApiModelProperty("账户颜色")
    private String accountColor;
    /**
     * 账户app图标
     */
    @NotBlank(message = "账户app图标")
    @ApiModelProperty("账户app图标")
    private String accountAppIcon;
    /**
     * 年份
     */
    @ApiModelProperty("年份")
    private String yearStr;
    /**
     * 年月
     */
    @ApiModelProperty("年月")
    private String yearMonthStr;
    /**
     * 月份
     */
    @ApiModelProperty("月份")
    private String monthStr;
    /**
     * 天
     */
    @ApiModelProperty("天")
    private String dayStr;
    /**
     * 天 yyyy-MM-dd格式
     */
    @ApiModelProperty("天 yyyy-MM-dd格式")
    private String dayString;
    /**
     * 日期（时间戳）
     */
    @ApiModelProperty("日期（时间戳）")
    private Long dayTimestamp;
    /**
     * 周几
     */
    @ApiModelProperty("周几")
    private String weekStr;
    /**
     * 账单时间（MM月dd日）
     */
    @ApiModelProperty("账单时间（MM月dd日）")
    private String dayTime;
    /**
     * 分秒字符串（mm:dd）格式
     */
    @ApiModelProperty("分秒字符串（mm:dd）格式")
    private String hourMinutes;
    /**
     * 备注
     */
    @ApiModelProperty("备注")
    private String remarks;

    /**
     * 新增前初始化
     */
    public void initProperties() {
        Calendar instance = Calendar.getInstance();
        instance.setTime(this.time);
        this.monthStr = instance.get(Calendar.MONTH) + 1 + StringUtils.EMPTY;
        this.yearStr = instance.get(Calendar.YEAR) + StringUtils.EMPTY;
        this.dayStr = instance.get(Calendar.DATE) + Strings.EMPTY;
        this.yearMonthStr = this.yearStr + Constants.H + this.monthStr;
        this.weekStr = Constants.WEEK_MAP.get(instance.get(Calendar.DAY_OF_WEEK));
        String format = new SimpleDateFormat(Constants.YYYY_MM_DD_HH_MM_SS).format(this.time);
        String dayTimeStr = format.substring(5, 10);
        String hourMinutesStr = format.substring(11, 16);
        String[] split = dayTimeStr.split(Constants.H);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            if (i == 0) {
                stringBuilder.append(split[i]).append("月");
            } else {
                stringBuilder.append(split[i]).append("日");
            }
        }
        this.dayTime = stringBuilder.toString();
        this.hourMinutes = hourMinutesStr;
        this.userId = ThreadLocalUtil.getUserId();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtils.YYYY_MM_DD);
        this.dayString = simpleDateFormat.format(this.time);
        try {
            this.dayTimestamp = simpleDateFormat.parse(this.dayString).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
